const {
  buildResponse,
  successFormat,
  errorFormat,
  gzip,
  common,
} = require("./common.js");
exports.done = function (app) {
  app
    .route("/fetchWebContent")
    .all(function (req, res, next) {
      gzip(req, res);
      common(req, res);
      next();
    })
    .get(function (req, res, next) {
      app.$store
        .dispatch("fetchWebContent", req.query)
        .then((data) => {
          res.end(buildResponse(req, successFormat(200, [])));
        })
        .catch((err) => {
          console.error(err);
          res.end(buildResponse(req, errorFormat(0)));
        });
    });
  app
    .route("/fetchSqlData")
    .all(function (req, res, next) {
      gzip(req, res);
      common(req, res);
      next();
    })
    .get(function (req, res, next) {
      app.$pool.query("SELECT * FROM user", (err, rows) => {
        if (err) {
          res.end(buildResponse(req, errorFormat(0, { err })));
          return;
        }
        res.end(buildResponse(req, successFormat(200, { err, rows })));
      });
    });
  app
    .route("/fetchRemotData")
    .all(function (req, res, next) {
      gzip(req, res);
      common(req, res);
      next();
    })
    .get(function (req, res, next) {
      app.$request
        .Get("/", {})
        .then((data) => {
          res.end(buildResponse(req, successFormat(200, data)));
        })
        .catch((err) => {
          res.end(buildResponse(req, errorFormat(0, { err })));
        });
    });
};
